
#include "aky_program/aky_com_include.h"
static void my_event_cb(lv_obj_t * obj, lv_event_t event);


static lv_obj_t *tc_area_back;          //
static lv_obj_t *tc_area_auto_train;          //自动训练
static lv_obj_t *tc_area_manual_train;          //手动训练
static lv_obj_t *tc_area_bp_assessment;          //血压评估
static lv_obj_t *tc_area_set;          //设置
static lv_obj_t *tc_area_use_tutori;          //使用教程
static lv_obj_t *tc_area_knowledge;          //科普知识
static lv_obj_t *tc_area_engineer;          //工程模式入口 连续点击


uint16_t engineer_click_cnt = 0;
/*
 * 界面回调函数
 */
static void screen005_on_timer(void *user_data)
{
    /* 界面定时器回调函数 */
    static uint16_t time_cnt = 0;
    static uint16_t engineer_click_cnt_last = 0;
    screen_flush_pro();
    if (engineer_click_cnt > 0)
    {
        if (engineer_click_cnt_last != engineer_click_cnt)
        {
            engineer_click_cnt_last = engineer_click_cnt;
            time_cnt = 0;
        }
        
        time_cnt++;
        if (time_cnt > 1500 / 10)
        {
            time_cnt = 0;
            engineer_click_cnt = 0;
        }
    }
    else
    {
        time_cnt = 0;
    }
    if (get_spk_recogni_info() == START_AUTO_TRAIN)
    {
        clr_spk_recogni_info();
        lv_event_send(tc_area_auto_train,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == START_MANUAL_TRAIN)
    {
        clr_spk_recogni_info();
        lv_event_send(tc_area_manual_train,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == BP_ASSESSMENT)
    {
        clr_spk_recogni_info();
        lv_event_send(tc_area_bp_assessment,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == ENTER_SET)
    {
        clr_spk_recogni_info();
        lv_event_send(tc_area_set,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == USE_TUTORIAL)
    {
        clr_spk_recogni_info();
        lv_event_send(tc_area_use_tutori,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == POPULAR_SCIENCE_KNOWLEDGE)
    {
        clr_spk_recogni_info();
        lv_event_send(tc_area_knowledge,LV_EVENT_CLICKED,NULL);
    }
}

static void screen005_start(synwit_id_t screen_id)
{
    /* 在界面被显示给用户前，这个接口会被调用。 */
    engineer_click_cnt = 0;

    tc_area_back = synwit_ui_find_lv_obj(WIDGET_TOUCH_5);
    tc_area_auto_train = synwit_ui_find_lv_obj(WIDGET_TOUCH_6);
    tc_area_manual_train = synwit_ui_find_lv_obj(WIDGET_TOUCH_7);
    tc_area_bp_assessment = synwit_ui_find_lv_obj(WIDGET_TOUCH_8);
    tc_area_set = synwit_ui_find_lv_obj(WIDGET_TOUCH_9);
    tc_area_use_tutori = synwit_ui_find_lv_obj(WIDGET_TOUCH_10);
    tc_area_knowledge = synwit_ui_find_lv_obj(WIDGET_TOUCH_11);
    tc_area_engineer = synwit_ui_find_lv_obj(WIDGET_TOUCH_36);

    lv_obj_set_event_cb(tc_area_back, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_auto_train, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_manual_train, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_bp_assessment, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_set, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_use_tutori, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_knowledge, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_engineer, my_event_cb);   /*Assign an event callback*/
    g_s_mem_index = flash_get_mem_index_data(g_e_user,&g_e_flash_err,MAIN_ADDR);
    if(g_s_mem_index.flag_is_user_info_valid == 0)
    {
        synwit_ui_load_screen(SCREEN003);
    }


    // 打开下面的注释可以为本界面开启一个每100ms触发一次的定时器
    screen_init();
    synwit_ui_start_scr_timer(10, NULL);
}

static void screen005_stop(synwit_id_t screen_id)
{
    /* 准备切换到其它界面前，这个接口会被调用。 */
    screen_stop();
}

static int screen005_favorite_stop_policy()
{
    /* 函数返回值表示本界面结束时，希望采用的资源管理策略 */

    /* 资源管理策略(主要影响本界面再次进入时的加载速度):
     *    0: 系统推荐，释放当前界面引用的资源，保留控件及控件状态。可获得平衡的界面切换性能和内存消耗
     *    1: 释放当前界面引用的资源，并删除所有控件。
     *    2: 保留当前界面的所有资源及控件。可以获得最佳的界面切换性能，但内存消耗较大
     */
    return 0;
}


/*
 * 界面注册对象
 */
synwit_screen_callback_t screen005_cb_obj = {
    .favorite_stop_policy = screen005_favorite_stop_policy,
    .on_screen_start = screen005_start,
    .on_screen_stop = screen005_stop,
    .on_screen_timer = screen005_on_timer,
};


static void my_event_cb(lv_obj_t * obj, lv_event_t event)
{
    off_time_clr();
    if (obj == tc_area_back)
    {
        if (event == LV_EVENT_CLICKED)
        {
            synwit_ui_load_screen(SCREEN003);
        }
    }
    if (obj == tc_area_engineer)
    {
        if (event == LV_EVENT_CLICKED)
        {
            engineer_click_cnt++;
            printf("engineer_click_cnt = %d\r\n", engineer_click_cnt);
            if (engineer_click_cnt >= 5)
            {
                engineer_click_cnt = 0;
                synwit_ui_load_screen(SCREEN012);
            }
        }
    }    
    if (obj == tc_area_set)
    {
        if (event == LV_EVENT_CLICKED)
        {
            synwit_ui_load_screen(SCREEN006);
        }
    }
    if (obj == tc_area_use_tutori)
    {
        if (event == LV_EVENT_CLICKED)
        {
            synwit_ui_load_screen(SCREEN007);
        }
    }
    if (obj == tc_area_auto_train)
    {
        if (event == LV_EVENT_CLICKED)
        {
            synwit_ui_load_screen(SCREEN008);
        }
    }
    if (obj == tc_area_bp_assessment)
    {
        if (event == LV_EVENT_CLICKED)
        {
            synwit_ui_load_screen(SCREEN009);
        }
    }
    if (obj == tc_area_manual_train)
    {
        if (event == LV_EVENT_CLICKED)
        {
            synwit_ui_load_screen(SCREEN010);
        }
    }
    if (obj == tc_area_knowledge)
    {
        if (event == LV_EVENT_CLICKED)
        {
            synwit_ui_load_screen(SCREEN011);
        }
    }
}

